package com.isight.dao.jpa;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.isight.dao.AuthorityDao;
import com.isight.domain.Authority;

@Repository("authorityDao")
public class AuthorityDaoJpa implements AuthorityDao {

    private EntityManager entityManager;
    
    @PersistenceContext
    public void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }
    
    @SuppressWarnings("unchecked")
    @Transactional(readOnly = true)
    @Override
    public List<Authority> getAuthoritiesByUsername(String username)
            throws DataAccessException {
        Query jpqlQuery = entityManager.createQuery(
                "select a from Authority a where a.username = :name",
                Authority.class);
        jpqlQuery.setParameter("name", username);
        return jpqlQuery.getResultList();
    }

}
